<template>
  <el-form :model="detailForm" ref="detailForm" :inline="false"
           :label-position="labelPosition" :class="isEdit?'':'formItem'">

    <!-- 其他保险 -->
    <el-card class="box-card" shadow="never">

      <div slot="header" class="header-card">
        <span class="header-card-title" style="padding-right: 15px">其他非车险</span>
        <el-switch v-model="detailForm.otherOpenStatus" :active-value="1" :inactive-value="0"
                   active-color="#1890FF" v-if="isEdit && switchBoole"
                   inactive-color="#CDCDCD">
        </el-switch>
      </div>

      <el-row :gutter="24">
        <el-col :span="8">
          <!--:rules="[{required: required,message: '请输入其他保险备注',trigger: 'blur'}]"-->
          <el-form-item label="其他非车险备注:" prop="otherInsuranceRemark">
            <el-input v-model="detailForm.otherInsuranceRemark"
                      :disabled="detailForm.otherOpenStatus == 0" maxlength="50" v-if="isEdit"
                      placeholder="请输入其他非车险备注"></el-input>
            <span v-else>{{ detailForm.otherInsuranceRemark }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <!--:rules="[{required: required,message: '其他保险保费合计',trigger: 'blur'}]"-->
          <el-form-item label="其他非车险保费合计:" prop="otherInsuranceSumAmount">
            <el-input v-model="detailForm.otherInsuranceSumAmount" v-if="isEdit"
                      :disabled="detailForm.otherOpenStatus == 0"
                      v-thousand
                      @change="changeAmount('otherInsuranceSumAmount')"
                      placeholder="请输入其他非车险保费合计"></el-input>
            <span v-else>{{ format_thousand(detailForm.otherInsuranceSumAmount) }}</span>
          </el-form-item>
        </el-col>

        <el-col>
          <el-form-item label="附件:" v-if="detailForm.insuranceChannels == 1 ">
            <image-common v-model="detailForm.attachmentUrl" :limit="20" :disabled="!isEdit" :upType="2"/>
          </el-form-item>
        </el-col>

      </el-row>
    </el-card>

  </el-form>
</template>

<script>
export default {
  name: "other", //其他保险

  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    },
    switchBoole: {
      type: Boolean,
      default: false,
    }
  },

  computed: {
    detailForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.handleData(key, value);
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === "write") {
        return "top";
      } else {
        return "right";
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool;
    },
  },

  data() {
    return {
      required: false,
      switchValue: false,
    };
  },

  created() {
  },

  methods: {
    handleSwitch(val) {
      if (val && this.state === "write") {
        this.required = true;
      } else {
        this.required = false;
      }
    },

    changeAmount(str) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(this.detailForm[str])) {
        this.detailForm[str] = "";
      } else {
        this.detailForm[str] = this.detailForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }
    },
    handleData(key, value) {
      this.value.insuranceVoList.forEach((item) => {
        item[key] = value;
      });
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        resolve();
        // if (this.required) {
        //   this.$refs.detailForm.validate((valid) => {
        //     if (valid) {
        //       resolve();
        //     } else {
        //       this.locateToErr();
        //       reject();
        //     }
        //   });
        // } else {
        //   resolve();
        // }
      });
    },

    locateToErr() {
      this.$nextTick(() => {
        let isError = document.getElementsByClassName("is-error");
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: "center",
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: "smooth",
        });
      });
    },

    valid() {
      return new Promise((resolve) => {
        if (this.required) {
          this.$refs.detailForm.validate((valid) => {
            resolve(valid);
          });
        } else {
          resolve(true);
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>
